import '../../helper/Helper.dart';

import '../helper/dioHelper2.dart';

class DCSSData {
  num ID;
  String? TM;
  double H1;
  double H2;
  double R;
  double Qf;
  double Qp;
  double Pz;
  double P1;
  double P2;
  double P3;
  double P4;
  double Q1;
  double Q2;
  double Q3;
  double Q4;

  DCSSData(
      this.ID,
      this.TM,
      this.H1,
      this.H2,
      this.R,
      this.Qf,
      this.Qp,
      this.Pz,
      this.P1,
      this.P2,
      this.P3,
      this.P4,
      this.Q1,
      this.Q2,
      this.Q3,
      this.Q4);
  DCSSData.fromJson(Map<String, dynamic> json)
      : ID = json["ID"],
        TM = json["TM"],
        H1 = json["H1"] * 1.0,
        H2 = json["H2"] * 1.0,
        R = json["R"] * 1.0,
        Qf = json["Qf"] * 1.0,
        Qp = json["Qp"] * 1.0,
        Pz = json["Pz"] * 1.0,
        P1 = json["P1"] * 1.0,
        P2 = json["P2"] * 1.0,
        P3 = json["P3"] * 1.0,
        P4 = json["P4"] * 1.0,
        Q1 = 0,
        Q2 = 0,
        Q3 = 0,
        Q4 = 0;

  Map<String, dynamic> toJson() => {
        'ID': ID,
        'TM': TM,
        'H1': H1,
        'H2': H2,
        'R': R,
        'Qf': Qf,
        'Qp': Qp,
        'Pz': Pz,
        'P1': P1,
        'P2': P2,
        'P3': P3,
        'P4': P4,
      };
  static Future<bool> Add(DCSSData x1) async {
    var response = await DioHelper.Post("/DCSSData/Add", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Update(DCSSData x1) async {
    var response = await DioHelper.Post("/DCSSData/Update", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Delete(DCSSData x1) async {
    var st = x1.toJson();
    var response = await DioHelper.Post("/DCSSData/Delete", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<DCSSData?> Get() async {
    var response = await DioHelper.Get2(
        "http://47.108.62.6:27701/api/v1", "/DC/SSDataGet", {});
    print(response.data);
    if (response.statusCode == 200) {
      var x1 = DCSSData.fromJson(response.data);
      x1.P1 = x1.P1 / 10;
      x1.P2 = x1.P2 / 10;
      x1.P3 = x1.P3 / 10;
      x1.P4 = x1.P4 / 10;
      x1.Pz = x1.Pz / 10;

      var pz = x1.P1 + x1.P2 + x1.P3 + x1.P4;
      // ignore: prefer_conditional_assignment
      if (x1.TM == null) {
        x1.TM = Helper.DateTimeNowString();
      }
      x1.Q1 = x1.Qf * x1.P1 / pz;
      x1.Q2 = x1.Qf * x1.P2 / pz;
      x1.Q3 = x1.Qf * x1.P3 / pz;
      x1.Q4 = x1.Qf * x1.P4 / pz;

      return x1;
    }

    return null;
  }

  static Future<List<DCSSData>> GetList(
      String where, dynamic data, int pageIndex, int pageSize) async {
    var response = await DioHelper.Post("/DCSSData/GetListByWhere", {
      "Where": where,
      "obj": data,
      "PageIndex": pageIndex,
      "PageSize": pageSize,
    });
    var list1 = <DCSSData>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(new DCSSData.fromJson(x));
      }
    }
    return list1;
  }

  static Future<List<DCSSData>> GetAllList() async {
    var response = await DioHelper.Get("/DCSSData/GetAllList", {});
    var list1 = <DCSSData>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(new DCSSData.fromJson(x));
      }
    }
    return list1;
  }
}
